<template>
  <div>
    <el-tree
      ref="tree"
      check-strictly
      show-checkbox
      :data="list"
      node-key="id"
      :props="{label:'name'}"
      default-expand-all
    />

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="$emit('close')">取消</el-button>
    </div>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permisson'
import { tranDataTree } from '@/utils'
import { getRoleDetails, updataAssignPrem } from '@/api/settings'
export default {
  props: {
    id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    hSubmit() {
      const permIds = this.$refs.tree.getCheckedKeys()
      updataAssignPrem({ id: this.id, permIds })
      // console.log(permIds, 'perrrrrrrr')
      this.$emit('close')
    },
    // 获取所有的权限节点
    async loadPermissionList() {
      try {
        // console.log(this.id)
        const res = await getPermissionList()
        // console.log('getPermissionList', res)
        this.list = tranDataTree(res.data)
        // this.$refs.tree.setCheckedKeys([3])
        const info = await getRoleDetails(this.id)
        // console.log(info, '-=--------')
        this.$refs.tree.setCheckedKeys(info.data.permIds)
        // 保存数据
      } catch (err) {
        console.log('getPermissionList ', err)
      }
    }
  }
}
</script>

<style>

</style>
